package com.oasys.service.impl;

import com.oasys.dao.RoleMapper;
import com.oasys.entity.dto.RoleUserDto;
import com.oasys.entity.po.RoleMenuPo;
import com.oasys.entity.po.RolePo;
import com.oasys.entity.vo.RoleUserVo;
import com.oasys.entity.vo.RoleVo;
import com.oasys.enums.OaResponseCode;
import com.oasys.service.RoleService;
import com.oasys.utils.AutoGenerateIds;
import com.oasys.utils.ResponseResult;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2022/5/26
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public List<RoleUserDto> getRoleUserInfo() {
        return roleMapper.getRoleUserInfo();
    }

    /**
     * 保存角色菜单信息
     * @param roleUserVo
     * @return
     */
    @Override
    public ResponseResult<Boolean> saveMenuRoleInfo(RoleUserVo roleUserVo) {
        ResponseResult<Boolean> result1 = new ResponseResult<>();
        for (Long menuId : roleUserVo.getMenuIds()) {
            RoleMenuPo roleMenus = roleMapper.getRoleMenus(roleUserVo.getRoleId(), menuId);
            if (ObjectUtils.isEmpty(roleMenus)){
                Boolean result = roleMapper.saveMenuRoleInfo(AutoGenerateIds.getGenerateId(), roleUserVo.getRoleId(), menuId);
                if (!result){
                    result1.setCode(OaResponseCode.SUCCESS.getCode());
                    result1.setMsg(OaResponseCode.ROLE_MENU_ADD_FAIL.getMsg());
                    result1.setData(false);
                }
                result1.setCode(OaResponseCode.SUCCESS.getCode());
                result1.setMsg(OaResponseCode.ROLE_MENU_ADD_SUCCESS.getMsg());
                result1.setData(true);
            }else {
                result1.setCode(OaResponseCode.SUCCESS.getCode());
                result1.setMsg(OaResponseCode.ROLE_MENU_EXIST.getMsg());
                result1.setData(false);
            }
        }
        return result1;
    }

    /**
     * 获取角色编码
     * @param userId id
     * @return int
     */
    @Override
    public Integer getRoleNo(Long userId) {
        return roleMapper.getRoleNo(userId);
    }

    /**
     * 获取所有角色
     * @param roleName name
     * @return list
     */
    @Override
    public ResponseResult<List<RolePo>> getAllRoleInfo(String roleName) {
        ResponseResult<List<RolePo>> result = new ResponseResult<>();
        List<RolePo> roleList = roleMapper.getAllRoleInfo(roleName);
        result.setCode(OaResponseCode.SUCCESS.getCode());
        result.setData(roleList);
        return result;
    }

    @Override
    public ResponseResult<Boolean> addRole(String roleName) {
        Integer maxRoleNo = roleMapper.getMaxRoleNo();
        RolePo rolePo = new RolePo();
        rolePo.setRoleId(AutoGenerateIds.getGenerateId());
        rolePo.setRoleName(roleName);
        rolePo.setFlagInvalid(0);
        rolePo.setRoleNo(maxRoleNo + 1);
        Boolean role = roleMapper.saveRole(rolePo);
        return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),null,role);
    }

    @Override
    public ResponseResult<Boolean> updateRole(RoleVo roleVo) {
        Boolean result = roleMapper.updateRole(roleVo);
        return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),null,result);
    }

    @Override
    public ResponseResult<Boolean> delRole(Long roleId) {
        Boolean result = roleMapper.delRole(roleId);
        return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),null,result);
    }
}
